home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 52 / Amiga Format AFCD52 (Issue 136, May 2000).iso / -serious- / programming / other / pmdev / e / demos / dynamic.e < prev    next >
Encoding:
Text File  |  2000-02-28  |  8.2 KB  |  252 lines

  1. /* -- ----------------------------------------------------------------- -- *
  2.  * -- Program.....: Dynamic.e                                           -- *
  3.  * -- Author......: Daniel Kasmeroglu <raptor@cs.tu-berlin.de>          -- *
  4.  * -- Description.: Port of an example by Henrik Isaksson               -- *
  5.  * -- ----------------------------------------------------------------- -- *
  6.  * -- Original header:                                                  -- *
  7.  * --                                                                   -- *
  8.  * --   $VER: Dynamic.c 1.1 (05.09.98)                                  -- *
  9.  * --                                                                   -- *
  10.  * --   Popup Menu example program                                      -- *
  11.  * --                                                                   -- *
  12.  * --   ©1996-1998 Henrik Isaksson                                      -- *
  13.  * --   All Rights Reserved.                                            -- *
  14.  * --                                                                   -- *
  15.  * -- ----------------------------------------------------------------- -- */
  16.  
  17. /* -- ----------------------------------------------------------------- -- *
  18.  * --                              Options                              -- *
  19.  * -- ----------------------------------------------------------------- -- */
  20.  
  21. OPT PREPROCESS       -> enable preprocessor
  22.  
  23.  
  24. /* -- ----------------------------------------------------------------- -- *
  25.  * --                              Modules                              -- *
  26.  * -- ----------------------------------------------------------------- -- */
  27.  
  28. MODULE 'libraries/popupmenu' ,
  29.        'intuition/intuition' ,
  30.        'utility/tagitem'     ,
  31.        'utility/hooks'       ,
  32.        'tools/inithook'      
  33.  
  34. MODULE 'popupmenu'
  35.  
  36.  
  37. /* -- ----------------------------------------------------------------- -- *
  38.  * --                           Declarations                            -- *
  39.  * -- ----------------------------------------------------------------- -- */
  40.  
  41. -> Just used to find out if the destructor is called.
  42. DEF glo_wascalled
  43.  
  44.  
  45. /* -- ----------------------------------------------------------------- -- *
  46.  * --                               Main                                -- *
  47.  * -- ----------------------------------------------------------------- -- */
  48.  
  49. ->»» PROC main
  50. PROC main()
  51. DEF ma_window    : PTR TO window
  52. DEF ma_menu      : PTR TO popupmenu
  53. DEF ma_imsg1     : PTR TO intuimessage
  54. DEF ma_imsg2     : intuimessage
  55. DEF ma_construct : hook
  56. DEF ma_destruct  : hook
  57. DEF ma_result
  58.  
  59.   -> init the random number generator
  60.   RndQ( 1928172 )
  61.  
  62.   glo_wascalled := FALSE
  63.   ma_result     := TRUE
  64.  
  65.   -> User Data - you can put anything you like to here.
  66.   inithook( ma_construct , {hoo_SubConstructFunc} )
  67.   inithook( ma_destruct  , {hoo_SubDestructFunc}  )
  68.  
  69.   popupmenubase := OpenLibrary( 'popupmenu.library', 9 )
  70.   IF popupmenubase <> NIL
  71.  
  72.     -> Declared at the end of this file.
  73.     ma_menu := glo_MakeTestMenu( ma_construct, ma_destruct )
  74.     IF ma_menu <> NIL
  75.  
  76.       -> Open a little window
  77.       ma_window := OpenWindowTagList( NIL,
  78.       [ WA_IDCMP       , IDCMP_CLOSEWINDOW OR IDCMP_MOUSEBUTTONS OR IDCMP_VANILLAKEY ,
  79.         WA_RMBTRAP     , TRUE            ,
  80.         WA_DRAGBAR     , TRUE            ,
  81.         WA_WIDTH       , 150             ,
  82.         WA_HEIGHT      , 100             ,
  83.         WA_LEFT        , 0               ,
  84.         WA_TOP         , 100             ,
  85.         WA_TITLE       , 'Dynamic Menus' ,
  86.         WA_CLOSEGADGET , TRUE            ,
  87.         TAG_END ] )
  88.  
  89.       IF ma_window <> NIL
  90.  
  91.         WHILE ma_result <> FALSE
  92.  
  93.           -> Wait for a message
  94.           WaitPort( ma_window.userport )
  95.  
  96.           -> Get the message
  97.           WHILE (ma_imsg1 := GetMsg( ma_window.userport )) <> NIL
  98.  
  99.             -> Copy the contents of it
  100.             CopyMem( ma_imsg1, ma_imsg2, SIZEOF intuimessage )
  101.  
  102.             -> Reply the message
  103.             ReplyMsg( ma_imsg1 )
  104.  
  105.             IF ma_imsg2.class = IDCMP_MOUSEBUTTONS
  106.               Pm_OpenPopupMenuA( ma_window, [ PM_MENU, ma_menu, TAG_END ] )
  107.             ELSEIF ma_imsg2.class = IDCMP_CLOSEWINDOW
  108.               -> See if the user wants to quit
  109.               ma_result := FALSE
  110.             ENDIF
  111.  
  112.           ENDWHILE
  113.  
  114.         ENDWHILE
  115.  
  116.         PrintF( 'The destructor hook was \scalled !\n', IF glo_wascalled <> FALSE THEN '' ELSE 'NOT ' )
  117.  
  118.         CloseWindow( ma_window )
  119.  
  120.       ELSE
  121.         PrintF( 'Window error !\n' )
  122.       ENDIF
  123.       Pm_FreePopupMenu( ma_menu )
  124.     ELSE
  125.       PrintF( 'Menu error !\n' )
  126.     ENDIF
  127.  
  128.     CloseLibrary( popupmenubase )
  129.  
  130.   ELSE
  131.     PrintF( 'Cannot open "popupmenu.library" v9+ !\n' )
  132.   ENDIF
  133.  
  134. ENDPROC
  135. ->»»>
  136.  
  137.  
  138. /* -- ----------------------------------------------------------------- -- *
  139.  * --                             Functions                             -- *
  140.  * -- ----------------------------------------------------------------- -- */
  141.  
  142. ->»» PROC glo_MakeTestMenu
  143. PROC glo_MakeTestMenu( mak_construct, mak_destruct )
  144. DEF mak_menu : PTR TO popupmenu
  145.  
  146.   mak_menu := PMStartMenu,
  147.                 PMItem( 'Dynamic submenu' ),
  148.                   PM_SUBCONSTRUCT , mak_construct ,
  149.                   PM_SUBDESTRUCT  , mak_destruct  ,
  150.                   PMSimpleSub,
  151.                     PMInfo( 'This won\at work unless' ), PMEnd, -> this will be shown if the demo doesn't work
  152.                     PMInfo( 'you get version 7.50 !'  ), PMEnd, -> (maybe an old version of the library)
  153.                   PMEnd,
  154.                 PMEnd,
  155.               PMEnd
  156.  
  157. ENDPROC mak_menu
  158. ->»»>
  159.  
  160.  
  161. /* -- ----------------------------------------------------------------- -- *
  162.  * --                          Hook-Functions                           -- *
  163.  * -- ----------------------------------------------------------------- -- */
  164.  
  165. ->»» HOOK hoo_SubConstructFunc
  166. PROC hoo_SubConstructFunc( sub_hook, sub_selected : PTR TO popupmenu, sub_handle )
  167. DEF sub_newpm       : PTR TO popupmenu
  168. DEF sub_bfr [ 128 ] : STRING
  169.  
  170. -> Don't try to open windows, requesters or print text from this
  171. -> hook, wich is called while the menu is still open. (ofcourse)
  172. -> The display might be locked!
  173.  
  174.   sub_selected := sub_selected.ppm_Sub
  175.  
  176.   -> A random number to prove that it works...
  177.   StringF( sub_bfr, 'Random number: \d', Rnd( 1000 ) )
  178.  
  179.   sub_newpm := Pm_MenuA( [ PM_DUMMY, 0,
  180.                  PMItem( sub_bfr ), PM_ID, 200, PMEnd,
  181.                End
  182.  
  183.   IF sub_newpm <> NIL
  184.  
  185.     IF sub_selected.ppm_Sub <> NIL
  186.  
  187.       -> We don't have to worry about freeing the new menu,
  188.       -> as it will be freed automatically. But we do have
  189.       -> to take care of the old menu, since the library
  190.       -> won't know it exists.
  191.       Pm_FreePopupMenu( sub_selected.ppm_Sub )
  192.  
  193.     ENDIF
  194.  
  195.     sub_selected.ppm_Sub := sub_newpm
  196.  
  197.     ->
  198.     -> We can also add items to a previous menu
  199.     ->
  200.     Pm_InsertMenuItemA( sub_newpm,
  201.     [ PM_INSERT_LAST, TRUE,
  202.       PM_INSERT_ITEM, Pm_ItemA( [ PM_TITLE, 'These items have been added'  , TAG_END ] ),
  203.       PM_INSERT_ITEM, Pm_ItemA( [ PM_TITLE, 'to the end of the menu!'      , TAG_END ] ),
  204.       PM_INSERT_ITEM, Pm_ItemA( [ PM_TITLE, 'This item will be removed...' , PM_ID, 100, TAG_END ] ),
  205.       TAG_END ] )
  206.  
  207.     ->
  208.     -> Let's remove an item too:
  209.     ->
  210.     Pm_RemoveMenuItem( sub_newpm, Pm_FindItem( sub_newpm, 100 ) )
  211.  
  212.     ->
  213.     -> Let's add another one!
  214.     ->
  215.     Pm_InsertMenuItemA( sub_newpm,
  216.     [ PM_INSERT_AFTERID , 200,
  217.       PM_INSERT_ITEM    , Pm_ItemA( [ PM_TITLE, ',,^..^,,', TAG_END ] ),
  218.       TAG_END ] )
  219.  
  220.   ENDIF
  221.  
  222.   -> If you return NULL, no menu will be opened.
  223. ENDPROC sub_selected.ppm_Sub
  224. ->»»>
  225.  
  226. ->»» HOOK hoo_SubDestructFunc
  227. PROC hoo_SubDestructFunc( sub_hook, sub_parent : PTR TO popupmenu, sub_handle )
  228.  
  229. -> Don't try to open windows, requesters or print text from this
  230. -> hook, wich is called while the menu is still open. (of course)
  231. -> The display might be locked!
  232.  
  233. ->
  234. -> Here you can do something with your item's userdata...
  235. ->
  236. ->      sub_parent.ppm_Sub.ppm_UserData := 0
  237. ->
  238. -> ...or free the submenu, if you want to. Otherwise it will be free'd
  239. -> when you free the rest of the menu. If you decide to free the menu,
  240. -> remember to set sub_parent.ppm_Sub to NIL.
  241. ->
  242. ->      Pm_FreePopupMenu( sub_parent.ppm_Sub )
  243. ->      sub_parent.ppm_Sub := NIL
  244. ->
  245.  
  246.   -> Only to find out if this works...
  247.   glo_wascalled := TRUE
  248.  
  249. ENDPROC
  250. ->»»>
  251.  
  252.